package com.example.materialtest;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by 李明洋~ on 2018/3/11.
 */

public class FuritAdapter extends RecyclerView.Adapter<FuritAdapter.ViewHolder> {
    private Context context;
    private List<Furit> mfuritlist;
    static  class ViewHolder extends RecyclerView.ViewHolder{
        //初始化控件
        CardView cardView;
        ImageView imageView;
        TextView textView;

        public ViewHolder(View view) {
            super(view);
            cardView=(CardView)view;
            imageView=(ImageView)view.findViewById(R.id.furit_image);
            textView=(TextView)view.findViewById(R.id.furit_name);
        }
    }
    public  FuritAdapter(List<Furit> furits){
        mfuritlist=furits;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (context==null){
            context=parent.getContext();
        }
        View view= LayoutInflater.from(context).inflate(R.layout.furit_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int postion=holder.getAdapterPosition();
                Furit furit=mfuritlist.get(postion);
                Intent intent=new Intent(context,FuritActivity.class);
                intent.putExtra(FuritActivity.FRUIT_NAME,furit.getName());
                intent.putExtra(FuritActivity.FRUIT_IMAGE_ID,furit.getImageId());
                context.startActivity(intent);
            }
        });
        //加载Recyclerview的布局文件
        return holder;
    }

    @Override
    public void onBindViewHolder(FuritAdapter.ViewHolder holder, int position) {
        //设置每一个item的文字属性
        Furit furit=mfuritlist.get(position);
        holder.textView.setText(furit.getName());
        Glide.with(context).load(furit.getImageId()).into(holder.imageView);
        //Glide的用法 Glide.with(context)传入一个context,Activity,Fragment参数
        // 最后调用load方法加载图片可以是Url地址也可以是本地路径或者是资源id
        // 最后调用into方法将图片设置到某一个具体的ImageView中就可以了
    }

    @Override
    public int getItemCount() {
        return mfuritlist.size();
    }
}
